package com.medusa.gruul.payment.service.handler.refund.notify;

import com.egzosn.pay.common.bean.PayMessage;
import com.medusa.gruul.common.web.handler.Handler;
import com.medusa.gruul.payment.service.mp.service.IPaymentRefundService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @author xiaoq
 * @Description
 * @date 2022-08-08 15:31
 */
@Slf4j
@RequiredArgsConstructor
public abstract class AbstractRefundNotifyHandler implements Handler<Void> {

    private final IPaymentRefundService paymentRefundService;

    @Override
    public Void handle(Object... params) {
        if (hasErrorParam(params, PayMessage.class)) {
            throw new IllegalArgumentException("param is not PayMessage");
        }
        PayMessage message = cast(params[0], PayMessage.class);
        String refundNo = refundNo(message.getPayMessage());
        log.debug("::退款回调::RefundNo::{}", refundNo);
        paymentRefundService.refundSuccess(true, refundNo, message);
        return null;
    }


    /**
     * 获取退款单号
     *
     * @param message 回调消息
     * @return 退款单号
     */
    protected abstract String refundNo(Map<String, Object> message);


}
